// let o = { sing: "sing" };
// // 如何将this指向o呢？
// let obj = {
//   name: "刘德华",
//   say: function (...args) {
//     console.log(args);
//     console.log(this); // this -> o
//   },
// };
// let res = obj.say.bind(o, [o.sing, 1, 2, 3]);
// res();

// 手动实现一个 bind
Function.prototype.myBind = function (context, ...args) {
  if (typeof this !== "function") {
    throw new Error(
      "Function.prototype.bind - what is trying to be bound is not callable"
    );
  }

  var self = this;
  var args = Array.prototype.slice.call(arguments, 1);

  var fNOP = function () {};

  var fBound = function () {
    var bindArgs = Array.prototype.slice.call(arguments);
    return self.apply(
      this instanceof fNOP ? this : context,
      args.concat(bindArgs)
    );
  };

  fNOP.prototype = this.prototype;
  fBound.prototype = new fNOP();
  return fBound;
};
let o = { sing: "sing" };
// 如何将this指向o呢？
let obj = {
  name: "刘德华",
  say: function (...args) {
    console.log(args);
    console.log(this); // this -> o
  },
};
let res = obj.say.myBind(o, [o.sing, 1, 2, 3]);
res();
